//
//  TriggerEngine.m
//  carcareIOS
//
//  Created by ileo on 15/12/18.
//  Copyright © 2015年 baozun. All rights reserved.
//

#import "TriggerEngine.h"
#import "AIUserDefaultObject.h"

#import "UserHelper.h"
#import "AIUserInfo.h"
#import "AISystemInfo.h"
#import "CalendarMgr.h"

#import "AILDCalendar.h"
#import "CommentHelper.h"
#import "AILDSummary.h"
#import "AIGestureLoc.h"

@interface AITrigger : AIUserDefaultObject

@property (nonatomic, copy) NSString *version;
@property (nonatomic, copy) NSString *date;
@property (nonatomic, assign) NSInteger uid;


@end

@implementation AITrigger

@end

@interface TriggerEngine ()

@property (nonatomic, strong) AITrigger *aiTrigger;
@property (nonatomic, strong) NSMutableArray *visiters;

@end

@implementation TriggerEngine

IMP_SINGLETON

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.aiTrigger = [[AITrigger alloc] init];
        [self initVisiters];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAccountLogout) name:USER_LOGOUT object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(check) name:USER_LOGIN object:nil];
        [self check];
    }
    return self;
}

-(void)initVisiters{
    self.visiters = [NSMutableArray arrayWithCapacity:5];
    [self.visiters addObject:[[TriggerCalender alloc] init]];
    [self.visiters addObject:[[TriggerComment alloc] init]];
    [self.visiters addObject:[[TriggerSummary alloc] init]];
    [self.visiters addObject:[[TriggerGesture alloc] init]];
}

-(void)check{
    if (self.aiTrigger.uid != USERINFO.userID) {
        self.aiTrigger.uid = USERINFO.userID;
        [self handleAccountUpdate];
    }
    if (![self.aiTrigger.version isEqualToString:[AISystemInfo sharedInstance].version]) {
        self.aiTrigger.version = [AISystemInfo sharedInstance].version;
        [self handlesVersionUpdate];
    }
    if (![self.aiTrigger.date isEqualToString:CALENDARMGR.currentDay]) {
        self.aiTrigger.date = CALENDARMGR.currentDay;
        [self handlesDateUpdate];
    }
}

-(void)addVisiter:(id<TriggerVister>)visiter{
    if (![self.visiters containsObject:visiter]) {
        [self.visiters addObject:visiter];
    }
}

-(void)removeVisiter:(id<TriggerVister>)visiter{
    if ([self.visiters containsObject:visiter]) {
        [self.visiters removeObject:visiter];
    }
}

#pragma mark - hangle
-(void)handleAccountLogout{
    for (id<TriggerVister> visiter in self.visiters) {
        if ([visiter respondsToSelector:@selector(triggerByAccountLogout)]){
            [visiter triggerByAccountLogout];
        }
    }
}
-(void)handleAccountUpdate{
    for (id<TriggerVister> visiter in self.visiters) {
        if ([visiter respondsToSelector:@selector(triggerByAccountUpdate)]){
            [visiter triggerByAccountUpdate];
        }
    }
}
-(void)handlesVersionUpdate{
    for (id<TriggerVister> visiter in self.visiters) {
        if ([visiter respondsToSelector:@selector(triggerByVersionUpdate)]){
            [visiter triggerByVersionUpdate];
        }
    }
}
-(void)handlesDateUpdate{
    for (id<TriggerVister> visiter in self.visiters) {
        if ([visiter respondsToSelector:@selector(triggerByDateUpdate)]){
            [visiter triggerByDateUpdate];
        }
    }
}


@end
